using System;
using System.Xml;

namespace Mbs.CastleContainer
{
    abstract class AbstractStatementElementProcessor : AbstractXmlNodeProcessor
    {
        private static readonly String DefinedAttrName = "defined";
        private static readonly String NotDefinedAttrName = "not-defined";

        public AbstractStatementElementProcessor()
        {
        }

        protected bool ProcessStatement(XmlElement element, IXmlProcessorEngine engine)
        {
            if (!element.HasAttribute(DefinedAttrName) &&
                !element.HasAttribute(NotDefinedAttrName))
            {
                throw new XmlProcessorException("'if' elements expects a non empty defined or not-defined attribute");
            }

            if (element.HasAttribute(DefinedAttrName) &&
                element.HasAttribute(NotDefinedAttrName))
            {
                throw new XmlProcessorException("'if' elements expects a non empty defined or not-defined attribute");
            }

            bool processContents = false;

            if (element.HasAttribute(DefinedAttrName))
            {
                processContents = engine.HasFlag(element.GetAttribute(DefinedAttrName));
            }
            else
            {
                processContents = !engine.HasFlag(element.GetAttribute(NotDefinedAttrName));
            }

            return processContents;
        }
    }
}
